
package com.monlyu.util;

import java.util.List;

import net.sf.json.JSONObject;

import org.apache.struts2.json.JSONException;
import org.apache.struts2.json.JSONUtil;

/********************************************************************************
 * <b>功能描述☞</b>Json的操作类,进一步封住org.apache.struts2.json.JSONUtil<br>
 * <b>开发公司☞</b>重庆唯尼科技有限公司<br>
 * <b>版权☞</b>本文件归重庆唯尼科技有限公司所有<br>
 * <b>项目名☞</b>monlyu<br>
 * <b>文件路径☞</b>com.monlyu.util.JsonUtils.java<br>
 * <b>开发日期☞</b>2010-1-17 <br>
 * 
 * @author monlyu
 * @since JDK 5
 * @version 1.0.5
 ********************************************************************************/
public class JsonUtil {

    public static String obj2Json(Object obj){
        String value = "";
        try {
            value = JSONUtil.serialize(obj);
        }
        catch (JSONException e) {
            LogUtil.logWithDate(JsonUtil.class, "把对象转化为Json对象的时候发生异常", e);
        }
        return value;
    }

    public static String getJsonVal(JSONObject json, String key, String deft){
        try {
            return json.getString(key);
        }
        catch (Exception e) {}
        return deft;
    }

    public static Long getJsonLong(JSONObject json, String key, Long deft){
        try {
            return json.getLong(key);
        }
        catch (Exception e) {}
        return deft;
    }

    @SuppressWarnings("unchecked")
    public static List<JSONObject> getListItm(JSONObject json, String key, List<JSONObject> emptyList){
        try {
            return json.getJSONArray(key);
        }
        catch (Exception e) {}
        return emptyList;
    }

    public static int getJsonIntVal(JSONObject json, String key, int deft){
        try {
            return json.getInt(key);
        }
        catch (Exception e) {}
        return deft;
    }
}
